﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using smsclictr.automation;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Management;

namespace OpalisAddOn.Properties
{
    [Activity("Agent Properties", Description = "Get or Set common Agent Properties", ShowFilters=false)]
    public class AgentProperties : IActivity
    {
        private String targetComputer;
        private String userName;
        private string password;

        public void Design(IActivityDesigner designer)
        {
            designer.AddInput("TargetComputer").WithComputerBrowser();
            designer.AddInput("UserName").NotRequired();
            designer.AddInput("Password").NotRequired().PasswordProtect();

            designer.AddInput("SiteCode").NotRequired();
            designer.AddInput("HTTP Port").NotRequired();
            designer.AddInput("ServerLocatorPoint").NotRequired();
            designer.AddInput("Internet MP").NotRequired();
            designer.AddInput("DNS Suffix").NotRequired();
            designer.AddInput("Cache Size").NotRequired();
            designer.AddInput("Cache Path").NotRequired();

            designer.AddOutput("SiteCode").AsString().WithDescription("SCCM Agent Site Code (3Digit)");
            designer.AddOutput("Agent Version").AsString().WithDescription("SCCM Agent Version");
            designer.AddOutput("HTTP Port").AsString().WithDescription("SCCM Agent HTTP Port");
            designer.AddOutput("Assigned MP").AsString().WithDescription("Assigned Management Point (always primary site)");
            designer.AddOutput("ServerLocatorPoint").AsString().WithDescription("ServerLocatorPoint");
            designer.AddOutput("Internet MP").AsString().WithDescription("Internet Management Point");
            designer.AddOutput("DNS Suffix").AsString().WithDescription("SCCM Agent DNS Suffix");
            designer.AddOutput("Cache Size").AsString().WithDescription("SCCM Agent Cache Size");
            designer.AddOutput("GUID").AsString().WithDescription("SCCM Agent GUID");
        }

        public void Execute(IActivityRequest request, IActivityResponse response)
        {
            SMSClient oClient;
            targetComputer = request.Inputs["TargetComputer"].As<string>();
            userName = request.Inputs["UserName"].AsString();
            password = request.Inputs["Password"].AsString();

            if (string.IsNullOrEmpty(userName))
            {
                oClient = new SMSClient(targetComputer);
            }
            else
            {
                oClient = new SMSClient(targetComputer, userName, password);
            }

            if (request.Inputs.Contains("SiteCode"))
            {
                oClient.DeletePendingSiteAssignment();
                oClient.SiteCode = request.Inputs["SiteCode"].AsString();
            }
            if (request.Inputs.Contains("HTTP Port"))
                oClient.HttpPort = request.Inputs["HTTP Port"].AsString();
            if (request.Inputs.Contains("ServerLocatorPoint"))
                oClient.ServerLocatorPoint = request.Inputs["ServerLocatorPoint"].AsString();
            if (request.Inputs.Contains("Internet MP"))
                oClient.InternetMP = request.Inputs["Internet MP"].AsString();
            if (request.Inputs.Contains("DNS Suffix"))
                oClient.DNSSuffix = request.Inputs["DNS Suffix"].AsString();
            if (request.Inputs.Contains("Cache Size"))
                oClient.CacheSize = request.Inputs["Cache Size"].AsString();
            if (request.Inputs.Contains("Cache Path"))
                oClient.CachePath = request.Inputs["Cache Path"].AsString();

            response.Publish("SiteCode", oClient.SiteCode);
            response.Publish("Agent Version", oClient.SMSVersion);
            response.Publish("HTTP Port", oClient.HttpPort);
            response.Publish("Assigned MP", oClient.ManagementPoint);
            response.Publish("Proxy MP", oClient.ProxyManagementPoint);
            response.Publish("ServerLocatorPoint", oClient.ServerLocatorPoint);
            response.Publish("Internet MP", oClient.InternetMP);
            response.Publish("DNS Suffix", oClient.DNSSuffix);
            response.Publish("Cache Size", oClient.CacheSize);
            response.Publish("Cache Path", oClient.CachePath);
            response.Publish("GUID", oClient.ClientId);
        }
    }
}
